<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      @click="onAddComment"
      :disabled ='!message.length'
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/acticle'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: String,
      requried: true
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {},
  methods: {
    // 发布评论
    async onAddComment () {
      this.$toast.loading({
        message: '发布中',
        forbidClick: true,
        duration: 0
      })
      try {
        // 调用发布评论接口
        // 后端数据更新
        const { data } = await addComment({
          target: this.target, // 评论的目标id（评论文章即为文章id）
          content: this.message, // 评论内容
          art_id: this.$route.params.articleId
        })
        // console.log(data)
        // 视图更新
        // 清空文本
        this.message = ''
        // 关闭弹层
        // 发布内容显示列表项
        this.$toast.success('发布成功')
        this.$emit('post-success', data.data.new_obj)
      } catch (err) {
        this.$toast.fail('发布失败')
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
